package com.dingup.provider.controller.sys;

import com.dingup.api.Util.UuidUtil;
import com.dingup.api.base.HttpResContent;
import com.dingup.provider.cache.DingTalkTaskCache;
import com.dingup.provider.service.sys.SysUserService;
import com.suyeer.basic.util.BHttpResUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

import static com.dingup.api.base.BaseUtil.saveErrorMsgAndData;
import static com.dingup.api.base.BaseUtil.saveSuccessMsgAndData;

/**
 * Created by wancheng on 2018/7/27.
 */
@RestController()
@RequestMapping("/dingtalk")
public class SysDingtalkController {


    @Autowired
    SysUserService sysUserService;


    @RequestMapping(value = "/load/data.do",method = RequestMethod.POST)
    public void load(HttpServletRequest request, HttpServletResponse response,
                    String cacheId) {
        HttpResContent<Map<String,Object>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("加载钉钉信息");
        try {
            Map<String,Object> res = new HashMap();
            Boolean flag = false;
            if (StringUtils.isBlank(cacheId)){
                cacheId = UuidUtil.getUUID();
                DingTalkTaskCache.getInstance().put(cacheId,false);
                sysUserService.loadByDingTalkUser(cacheId);
            }
            flag = DingTalkTaskCache.getInstance().get(cacheId);
            res.put("cacheId",cacheId);
            res.put("result",flag);
            saveSuccessMsgAndData(hrc, res);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            BHttpResUtil.writeResult(hrc, true);
        }
    }
}
